package cn.com.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.FeignService;
import cn.com.entity.UserVo;

/**
 * @author liufl E-mail:Huberier@allpyra.com
 * @version 创建时间：2017年7月19日 下午2:51:51 类说明
 */
@EnableAutoConfiguration
@RefreshScope//配置中心参数刷新时也跟着刷新http://localhost:8081/refresh
@RestController
public class TestController {

	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	FeignService feignService;//通过feign调用远程rest服务

	/**
	 * 
	 * @return
	 */
	@RequestMapping("/discovery")
	public String doDiscoveryService() {
		StringBuilder buf = new StringBuilder();
		List<String> serviceIds = discoveryClient.getServices();
		log.info("__serviceIds=" + serviceIds.size());
		if (!CollectionUtils.isEmpty(serviceIds)) {
			for (String s : serviceIds) {
				System.out.println("serviceId:" + s);
				List<ServiceInstance> serviceInstances = discoveryClient
						.getInstances(s);
				if (!CollectionUtils.isEmpty(serviceInstances)) {
					for (ServiceInstance si : serviceInstances) {
						buf.append("[" + si.getServiceId() + " host="
								+ si.getHost() + " port=" + si.getPort()
								+ " uri=" + si.getUri() + "]");
					}
				} else {
					buf.append("no service.");
				}
			}
		}

		return buf.toString();
	}

	/**
	 * （http://localhost:8081/service-instances/CLOUD-CLIENT
	 * ）”CLOUD-CLIENT“是配置的应用名称的大写
	 * 
	 * @param applicationName
	 * @return
	 */
	@RequestMapping("/service-instances/{applicationName}")
	public List<ServiceInstance> serviceInstancesByApplicationName(
			@PathVariable String applicationName) {
		return this.discoveryClient.getInstances(applicationName);
	}

	@RequestMapping("/test")
	public String hello(HttpServletRequest req) {
		return "Hystrix!"+getUrl(req);
	}

	/**
	 * http://localhost:8081/feign/张三
	 * 会调用cloud-client项目的/testFeign/{name}方法
	 * @param name
	 * @param req
	 * @return
	 */
	@RequestMapping("/feign/{name}")
	public String feignHello(@PathVariable String name,HttpServletRequest req) {
		return getUrl(req)+"_client2_"+feignService.hello(name);
	}

	@RequestMapping("/local/{name}")
	public String apiHello(@PathVariable String name,HttpServletRequest req) {
		return getUrl(req)+"_client2_"+name;
	}

	@RequestMapping("/getAll")
	public List<UserVo> getAll(HttpServletRequest req) {
		List<UserVo> list = new ArrayList<>();
		list.add(new UserVo(123l, "张龙", getUrl(req)));
		list.add(new UserVo(456l, "赵虎", getUrl(req)));
		list.add(new UserVo(789l, "王朝", getUrl(req)));
		list.add(new UserVo(101l, "马汉", getUrl(req)));
		return list;
	}

	private String getUrl(HttpServletRequest req){
		String url=req.getScheme()+"://"+ req.getServerName()+":"+req.getServerPort()+"/"+req.getContextPath();
		return url;
	}
}
